﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VTV.TaskManagementSystem.BusinessLogic
{
    public class TaskTeam
    {
        public string ErrorMessage { get; set; }

        private DataAccess.TaskTeam _dataAccess;

        public bool AddNew(Entities.TaskTeam source)
        {
            try
            {
                if (!ValidationEntity(source))
                {
                    return false;
                }

                _dataAccess = new DataAccess.TaskTeam();
                var result = _dataAccess.AddNew(source);
                this.ErrorMessage = (!result) ? _dataAccess.ErrorMessage : null;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Delete(Entities.TaskTeam source)
        {
            try
            {
                if (!ValidationEntity(source))
                {
                    return false;
                }

                _dataAccess = new DataAccess.TaskTeam();
                var result = _dataAccess.Delete(source);
                this.ErrorMessage = (!result) ? _dataAccess.ErrorMessage : null;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public bool Update(Entities.TaskTeam source)
        {
            try
            {
                if (!ValidationEntity(source))
                {
                    return false;
                }

                _dataAccess = new DataAccess.TaskTeam();
                var result = _dataAccess.Update(source);
                this.ErrorMessage = (!result) ? _dataAccess.ErrorMessage : null;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return false;
            }
        }

        public IEnumerable<Entities.TaskTeamShow> GetTaskTeamShow(int taskID)
        {
            try
            {
                _dataAccess = new DataAccess.TaskTeam();
                var result = _dataAccess.GetTaskTeamShow(taskID);
                this.ErrorMessage = result == null ? _dataAccess.ErrorMessage : string.Empty;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public Entities.TaskTeam GetTaskTeam(int taskTeamID)
        {
            try
            {
                _dataAccess = new DataAccess.TaskTeam();
                var result = _dataAccess.GetTaskTeam(taskTeamID);
                this.ErrorMessage = result == null ? _dataAccess.ErrorMessage : string.Empty;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        private bool ValidationEntity(Entities.TaskTeam source)
        {
            try
            {
                //if ((source.ExpectedEndDate != null && !String.IsNullOrEmpty(source.ExpectedEndDate.Trim()) && source.TaskTeamName.Trim().Length > 200) ||
                //    (source.StartDate != null && !String.IsNullOrEmpty(source.Descriptions.Trim()) && source.Descriptions.Trim().Length > 200))
                //{
                //    ErrorMessage = Common.Constant.ValidationErrorMessage.OVERLOAD_LENGTH_OF_OBJECT;

                //  return false;
                //}

                return true;
            }
            catch (Exception e)
            {
                ErrorMessage = Common.Constant.ValidationErrorMessage.INVALID_VALUE + String.Format(" System message: {0}", e.Message);

                return false;
            }
        }

        public IEnumerable<Entities.ShowMe> GetDefaultToShow(DateTime? currentDate, string teamName, DateTime? startDate, DateTime? endDate)
        {
            try
            {
                _dataAccess = new DataAccess.TaskTeam();
                var result = _dataAccess.GetDefaultToShow(currentDate, teamName, startDate, endDate);
                this.ErrorMessage = result == null ? _dataAccess.ErrorMessage : string.Empty;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }

        public IEnumerable<Entities.ShowMeVer2> GetDefaultToShowVer2(DateTime? currentDate, string teamName, DateTime? startDate, DateTime? endDate)
        {
            try
            {
                _dataAccess = new DataAccess.TaskTeam();
                var result = _dataAccess.GetDefaultToShowVer2(currentDate, teamName, startDate, endDate);
                this.ErrorMessage = result == null ? _dataAccess.ErrorMessage : string.Empty;
                return result;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;

                return null;
            }
        }
    }
}
